package com.example.androidproject.utils;

import java.io.File;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class FileUploader {
    private static final MediaType MEDIA_TYPE_ZIP = MediaType.parse("application/zip");
    private final OkHttpClient client = new OkHttpClient();

    public void uploadZipFile(File zipFile, String serverUrl, Callback callback) {
        // 1. 创建请求体（Multipart格式）
        RequestBody fileBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart(
                        "file",  // 服务器接收文件的参数名
                        zipFile.getName(),
                        RequestBody.create(MEDIA_TYPE_ZIP, zipFile)
                )
                // 可以添加其他表单参数
                .addFormDataPart("description", "测试ZIP文件上传")
                .build();

        // 2. 创建请求
        Request request = new Request.Builder()
                .url(serverUrl)
                .post(fileBody)
                .build();

        // 3. 异步执行请求
        client.newCall(request).enqueue(callback);
    }
}
